/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.home.gui;

import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;
import com.home.common.ApplicationCommon;
import com.home.common.Constants;
import com.home.common.LevelSubject;
import com.home.common.utils.DateUtils;
import com.home.item.AgentComboItem;
import com.home.item.MonthComboItem;
import com.home.item.QuarterComboItem;
import com.home.item.SubjectComboItem;
import com.home.item.WeekComboItem;
import com.home.logic.AgentLogic;
import com.home.logic.ReportLogic;
import com.home.logic.ScorecardTypeLogic;
import com.home.logic.SubjectLogic;
import com.home.logic.TeamLogic;
import com.home.mapping.Agent;
import com.home.mapping.ScorecardType;
import com.home.mapping.Subject;
import com.home.mapping.Team;
import com.home.mapping.Venture;
import com.home.model.dto.ReportAVGScoreMapWeekDto;
import com.home.model.dto.ReportPerOfAttrMapDto;
import com.home.model.dto.ReportPerOfCateMapDto;
import com.home.model.dto.ReportTotalEvaluationMapWeekDto;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author vi
 */
public class QualityReport extends javax.swing.JInternalFrame {
    private List<Agent> listAgentQA = null;
    private List<Agent> listAgentTeamLead = null;    
    private int selectedGroup = Constants.GROUP_TYPE_AGENT;
    private Integer selectedChanelId = -1;
    private String selectedData = "";
    private int typePeriod = Constants.PERIOD_TYPE_WEEKLY;
    private Map<Integer,String> mapMonth = new HashMap<>();
    private int selectedLevelSubject;
    private MainScreen main;
    private Map<Integer,List<Subject>> mappSubjects = null;
    private Venture currentVenture = new Venture();
    private List<Agent> listAgent = null;
    /**
     * Creates new form QualityReport
     */
    public QualityReport(MainScreen main) {
        initComponents();
        this.main = main;
        currentVenture = main.getAgentLogin().getTeam().getVenture();
        currentVenture.setTeams(ImmutableSet.copyOf(new TeamLogic().findTeamsAndAgentsByVenture(currentVenture.getIdVenture())));
        
        setSize(new Dimension(main.getWidth() - 9, main.getHeight() - 95));
        
        groupValuePanel.setEnabled(false);
        btnExportToExcel.setEnabled(false);
        spinnerYearTo.setEnabled(false);
        
        loadDataChanels();
        loadDataDatas();
        
        radGroupAgent.setSelected(true);
        radPeriodWeekly.setSelected(true);
        loadWeeklyFromTo();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bGGroup = new javax.swing.ButtonGroup();
        bGPeriod = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jSplitPane1 = new javax.swing.JSplitPane();
        rightPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResultReport = new javax.swing.JTable();
        btnExportToExcel = new javax.swing.JButton();
        contentLeftPanel = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        leftPanel = new javax.swing.JPanel();
        panelContentChanels = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        panelChanels = new javax.swing.JPanel();
        panelGroups = new javax.swing.JPanel();
        radGroupAgent = new javax.swing.JRadioButton();
        radGroupQA = new javax.swing.JRadioButton();
        radGroupTeamLead = new javax.swing.JRadioButton();
        radGroupTypeOf = new javax.swing.JRadioButton();
        panelGroups1 = new javax.swing.JPanel();
        radPeriodWeekly = new javax.swing.JRadioButton();
        radPeriodMonthly = new javax.swing.JRadioButton();
        radPeriodQuarterly = new javax.swing.JRadioButton();
        btnReport = new javax.swing.JButton();
        cboData = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        groupValueContentPanel = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        groupValuePanel = new javax.swing.JPanel();
        chkCheckAll = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        spinnerYearFrom = new com.toedter.calendar.JYearChooser();
        jLabel6 = new javax.swing.JLabel();
        cboRangeFrom = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        cboRangeTo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        spinnerYearTo = new com.toedter.calendar.JYearChooser();

        setClosable(true);
        setTitle("Quality Report");
        setPreferredSize(new java.awt.Dimension(1300, 640));

        jSplitPane1.setDividerLocation(400);

        tblResultReport.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tblResultReport);

        btnExportToExcel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/excel.png"))); // NOI18N
        btnExportToExcel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportToExcelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout rightPanelLayout = new javax.swing.GroupLayout(rightPanel);
        rightPanel.setLayout(rightPanelLayout);
        rightPanelLayout.setHorizontalGroup(
            rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(rightPanelLayout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
                    .addComponent(btnExportToExcel)))
        );
        rightPanelLayout.setVerticalGroup(
            rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(rightPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 933, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExportToExcel))
        );

        jSplitPane1.setRightComponent(rightPanel);

        contentLeftPanel.setMinimumSize(new java.awt.Dimension(400, 600));
        contentLeftPanel.setPreferredSize(new java.awt.Dimension(300, 600));

        leftPanel.setPreferredSize(new java.awt.Dimension(275, 600));

        panelContentChanels.setBorder(javax.swing.BorderFactory.createTitledBorder("Channel"));
        panelContentChanels.setPreferredSize(new java.awt.Dimension(200, 107));
        panelContentChanels.setLayout(new javax.swing.BoxLayout(panelContentChanels, javax.swing.BoxLayout.Y_AXIS));

        jScrollPane2.setBorder(null);

        panelChanels.setLayout(new javax.swing.BoxLayout(panelChanels, javax.swing.BoxLayout.Y_AXIS));
        jScrollPane2.setViewportView(panelChanels);

        panelContentChanels.add(jScrollPane2);

        panelGroups.setBorder(javax.swing.BorderFactory.createTitledBorder("Group By"));
        panelGroups.setPreferredSize(new java.awt.Dimension(200, 107));
        panelGroups.setLayout(new javax.swing.BoxLayout(panelGroups, javax.swing.BoxLayout.Y_AXIS));

        bGGroup.add(radGroupAgent);
        radGroupAgent.setText("Agent");
        radGroupAgent.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        radGroupAgent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radGroupAgentActionPerformed(evt);
            }
        });
        panelGroups.add(radGroupAgent);

        bGGroup.add(radGroupQA);
        radGroupQA.setText("QA");
        radGroupQA.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        radGroupQA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radGroupQAActionPerformed(evt);
            }
        });
        panelGroups.add(radGroupQA);

        bGGroup.add(radGroupTeamLead);
        radGroupTeamLead.setText("Team Lead");
        radGroupTeamLead.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        radGroupTeamLead.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radGroupTeamLeadActionPerformed(evt);
            }
        });
        panelGroups.add(radGroupTeamLead);

        bGGroup.add(radGroupTypeOf);
        radGroupTypeOf.setText("Type of inquiry");
        radGroupTypeOf.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        radGroupTypeOf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radGroupTypeOfActionPerformed(evt);
            }
        });
        panelGroups.add(radGroupTypeOf);

        panelGroups1.setBorder(javax.swing.BorderFactory.createTitledBorder("Period"));
        panelGroups1.setPreferredSize(new java.awt.Dimension(200, 107));
        panelGroups1.setLayout(new javax.swing.BoxLayout(panelGroups1, javax.swing.BoxLayout.Y_AXIS));

        bGPeriod.add(radPeriodWeekly);
        radPeriodWeekly.setText("Weekly");
        radPeriodWeekly.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radPeriodWeeklyActionPerformed(evt);
            }
        });
        panelGroups1.add(radPeriodWeekly);

        bGPeriod.add(radPeriodMonthly);
        radPeriodMonthly.setText("Monthly");
        radPeriodMonthly.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radPeriodMonthlyActionPerformed(evt);
            }
        });
        panelGroups1.add(radPeriodMonthly);

        bGPeriod.add(radPeriodQuarterly);
        radPeriodQuarterly.setText("Quarterly");
        radPeriodQuarterly.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radPeriodQuarterlyActionPerformed(evt);
            }
        });
        panelGroups1.add(radPeriodQuarterly);

        btnReport.setText("Report");
        btnReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReportActionPerformed(evt);
            }
        });

        cboData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboDataActionPerformed(evt);
            }
        });

        jLabel1.setText("Data");

        groupValueContentPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("QA"));

        jScrollPane4.setMaximumSize(new java.awt.Dimension(200, 200));

        groupValuePanel.setMaximumSize(new java.awt.Dimension(200, 200));
        groupValuePanel.setLayout(new javax.swing.BoxLayout(groupValuePanel, javax.swing.BoxLayout.PAGE_AXIS));
        jScrollPane4.setViewportView(groupValuePanel);

        chkCheckAll.setText("Check All");
        chkCheckAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkCheckAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout groupValueContentPanelLayout = new javax.swing.GroupLayout(groupValueContentPanel);
        groupValueContentPanel.setLayout(groupValueContentPanelLayout);
        groupValueContentPanelLayout.setHorizontalGroup(
            groupValueContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(groupValueContentPanelLayout.createSequentialGroup()
                .addComponent(chkCheckAll)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        groupValueContentPanelLayout.setVerticalGroup(
            groupValueContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, groupValueContentPanelLayout.createSequentialGroup()
                .addComponent(chkCheckAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Range"));

        jLabel8.setText("Year From");

        jLabel6.setText("From");

        jLabel7.setText("To");

        jLabel2.setText("Year To");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel2))
                .addGap(14, 14, 14)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(spinnerYearFrom, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                    .addComponent(cboRangeTo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cboRangeFrom, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(spinnerYearTo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(spinnerYearFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(spinnerYearTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cboRangeFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(cboRangeTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout leftPanelLayout = new javax.swing.GroupLayout(leftPanel);
        leftPanel.setLayout(leftPanelLayout);
        leftPanelLayout.setHorizontalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, leftPanelLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnReport))
                    .addGroup(leftPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(panelContentChanels, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(leftPanelLayout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cboData, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, leftPanelLayout.createSequentialGroup()
                                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(panelGroups1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                                    .addComponent(panelGroups, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(groupValueContentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        leftPanelLayout.setVerticalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cboData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelContentChanels, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(leftPanelLayout.createSequentialGroup()
                        .addComponent(panelGroups, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panelGroups1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(groupValueContentPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnReport)
                .addContainerGap(74, Short.MAX_VALUE))
        );

        jScrollPane3.setViewportView(leftPanel);

        javax.swing.GroupLayout contentLeftPanelLayout = new javax.swing.GroupLayout(contentLeftPanel);
        contentLeftPanel.setLayout(contentLeftPanelLayout);
        contentLeftPanelLayout.setHorizontalGroup(
            contentLeftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
        );
        contentLeftPanelLayout.setVerticalGroup(
            contentLeftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
        );

        jSplitPane1.setLeftComponent(contentLeftPanel);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 984, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cboDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboDataActionPerformed
        selectedData = cboData.getSelectedItem().toString();
        if (selectedData.equals(Constants.QUALITY_REPORT_DATA_PER_CATE_PASSED_SCORE) 
                || selectedData.equals(Constants.QUALITY_REPORT_DATA_PER_ATTR_PASSED_SCORE)) {
            JRadioButton radAllChanel = (JRadioButton)panelChanels.getComponent(0);
            radAllChanel.setEnabled(false);
            JRadioButton radChanelInBound = (JRadioButton)panelChanels.getComponent(1);
            radChanelInBound.setEnabled(true);
            radChanelInBound.setSelected(true);
            JRadioButton radChanelDigital = (JRadioButton)panelChanels.getComponent(2);
            radChanelDigital.setEnabled(true);
            JRadioButton radChanelOutBound = (JRadioButton)panelChanels.getComponent(3);
            radChanelOutBound.setEnabled(true);
            
            JRadioButton radAllGroup = (JRadioButton)panelGroups.getComponent(0);
            radAllGroup.setSelected(true);
            radAllGroup.setEnabled(true);
            JRadioButton radGroupQA = (JRadioButton)panelGroups.getComponent(1);
            radGroupQA.setEnabled(true);            
            JRadioButton radGroupTeamLeader = (JRadioButton)panelGroups.getComponent(2);            
            radGroupTeamLeader.setEnabled(true);            
            JRadioButton radGroupSubject = (JRadioButton)panelGroups.getComponent(3);
            radGroupSubject.setEnabled(true);
            loadDataSelectGroupAgent();
            selectedChanelId = Integer.parseInt(radChanelInBound.getActionCommand());
        } else if (selectedData.equals(Constants.QUALITY_REPORT_DATA_AVG_SCORE)){
            JRadioButton radAllChanel = (JRadioButton)panelChanels.getComponent(0);
            radAllChanel.setEnabled(true);
            JRadioButton radChanelInBound = (JRadioButton)panelChanels.getComponent(1);
            radChanelInBound.setEnabled(true);
            JRadioButton radChanelDigital = (JRadioButton)panelChanels.getComponent(2);
            radChanelDigital.setEnabled(true);
            JRadioButton radChanelOutBound = (JRadioButton)panelChanels.getComponent(3);
            radChanelOutBound.setEnabled(true);
            
            radAllChanel.setSelected(true);
            JRadioButton radGroupAll = (JRadioButton)panelGroups.getComponent(0);
            radGroupAll.setEnabled(true);
            radGroupAll.setSelected(true);
            JRadioButton radGroupQA = (JRadioButton)panelGroups.getComponent(1);
            radGroupQA.setEnabled(true);
            JRadioButton radGroupTeamLead = (JRadioButton)panelGroups.getComponent(2);
            radGroupTeamLead.setEnabled(true);            
            JRadioButton radGroupSubject = (JRadioButton)panelGroups.getComponent(3);
            radGroupSubject.setEnabled(true);
            selectedChanelId = -1;
            loadDataSelectGroupAgent();
        } else {
            JRadioButton radGroupAgent = (JRadioButton)panelGroups.getComponent(0);
            radGroupAgent.setSelected(true);
            radGroupAgent.setEnabled(true);
            JRadioButton radGroupQA = (JRadioButton)panelGroups.getComponent(1);
            radGroupQA.setEnabled(true);                         
            JRadioButton radGroupTeamLead = (JRadioButton)panelGroups.getComponent(2);
            radGroupTeamLead.setEnabled(false);            
            JRadioButton radGroupSubject = (JRadioButton)panelGroups.getComponent(3);
            radGroupSubject.setEnabled(false);

            JRadioButton radChanelAll = (JRadioButton)panelChanels.getComponent(0);
            radChanelAll.setEnabled(true);
            radChanelAll.setSelected(true);
            JRadioButton radChanelInBound = (JRadioButton)panelChanels.getComponent(1);
            radChanelInBound.setEnabled(false);
            JRadioButton radChanelDigital = (JRadioButton)panelChanels.getComponent(2);
            radChanelDigital.setEnabled(false);
            JRadioButton radChanelOutBound = (JRadioButton)panelChanels.getComponent(3);
            radChanelOutBound.setEnabled(false);
            
            loadDataSelectGroupAgent();
            selectedChanelId = -1;
        }
    }//GEN-LAST:event_cboDataActionPerformed

    private void radPeriodWeeklyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radPeriodWeeklyActionPerformed
        loadWeeklyFromTo();
        typePeriod = Constants.PERIOD_TYPE_WEEKLY;
        spinnerYearTo.setEnabled(false);
    }//GEN-LAST:event_radPeriodWeeklyActionPerformed

    private void radPeriodMonthlyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radPeriodMonthlyActionPerformed
        loadMonthFromTo();
        typePeriod = Constants.PERIOD_TYPE_MONTHLY;
        spinnerYearTo.setEnabled(true);
    }//GEN-LAST:event_radPeriodMonthlyActionPerformed

    private void radPeriodQuarterlyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radPeriodQuarterlyActionPerformed
        loadQuarterFromTo();
        typePeriod = Constants.PERIOD_TYPE_QUARTERLY;
        spinnerYearTo.setEnabled(true);
    }//GEN-LAST:event_radPeriodQuarterlyActionPerformed

    private void btnReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReportActionPerformed
        int periodFrom = 0;
        int periodTo = 0;
        int yearFrom = spinnerYearFrom.getYear();
        int yearTo = spinnerYearTo.getYear();
        if (typePeriod == Constants.PERIOD_TYPE_WEEKLY) {
            WeekComboItem weekFromItem = (WeekComboItem) cboRangeFrom.getSelectedItem();
            periodFrom = weekFromItem.getValue();
            WeekComboItem weekToItem = (WeekComboItem) cboRangeTo.getSelectedItem();
            periodTo = weekToItem.getValue();
        } else if (typePeriod == Constants.PERIOD_TYPE_MONTHLY) {
            MonthComboItem monthFromItem = (MonthComboItem) cboRangeFrom.getSelectedItem();
            periodFrom = monthFromItem.getValue();
            MonthComboItem monthToItem = (MonthComboItem) cboRangeTo.getSelectedItem();
            periodTo = monthToItem.getValue();
        } else {
            QuarterComboItem quarterFromItem = (QuarterComboItem) cboRangeFrom.getSelectedItem();
            periodFrom = quarterFromItem.getValue();
            QuarterComboItem quarterToItem = (QuarterComboItem) cboRangeTo.getSelectedItem();
            periodTo = quarterToItem.getValue();
        }

        List<Integer> groupIdValues = new ArrayList<>();
        
            for (Component com : groupValuePanel.getComponents()) {
                JCheckBox chkCom = (JCheckBox) com;
                if (chkCom.isSelected()) {
                    groupIdValues.add(Integer.parseInt(chkCom.getActionCommand()));
                }
            }
            if (selectedGroup == Constants.GROUP_TYPE_TYPE_OF_INQUIRY) {
                if (selectedLevelSubject == LevelSubject.SUBJECT_LEVEL_1) {                    
                    List<Integer> listIdSubjectLv3 = new ArrayList<>();
                    for (Integer idSelected : groupIdValues) {                        
                        List<Subject> listSubjectChildLv2 = filterListChild(mappSubjects.get(LevelSubject.SUBJECT_LEVEL_2), idSelected);
                        for (Subject subjLv2 : listSubjectChildLv2) {
                            List<Subject> listSubjectChildLv3 = filterListChild(mappSubjects.get(LevelSubject.SUBJECT_LEVEL_3), subjLv2.getIdSubject());
                            if (listSubjectChildLv3.size() > 0) {
                                for (Subject subjLv3 : listSubjectChildLv3) {
                                    listIdSubjectLv3.add(subjLv3.getIdSubject());
                                }
                            } else {
                                listIdSubjectLv3.add(subjLv2.getIdSubject());
                            }                        
                        }
                    }                    
                    groupIdValues.clear();
                    groupIdValues.addAll(listIdSubjectLv3);
                } else if (selectedLevelSubject == LevelSubject.SUBJECT_LEVEL_2) {
                    List<Integer> listIdSubjectLv3 = new ArrayList<>();
                    for (Integer idSelected : groupIdValues) {                        
                        List<Subject> listSubjectChildLv3 = filterListChild(mappSubjects.get(LevelSubject.SUBJECT_LEVEL_3), idSelected);
                        if (listSubjectChildLv3.size() > 0) {
                            for (Subject subjLv3 : listSubjectChildLv3) {
                                listIdSubjectLv3.add(subjLv3.getIdSubject());
                            }
                        } else {
                            listIdSubjectLv3.add(idSelected);
                        }
                    }
                    groupIdValues.clear();
                    groupIdValues.addAll(listIdSubjectLv3);
                } else {
                    List<Integer> listIdSubjectLv3 = new ArrayList<>();
                    for (Integer idSelected : groupIdValues) {
                        for (Subject subLv3 : mappSubjects.get(LevelSubject.SUBJECT_LEVEL_3)) { 
                            if (subLv3.getIdSubject().compareTo(idSelected) == 0) {
                                listIdSubjectLv3.add(subLv3.getIdSubject());          
                            }
                        }
                    }
                    groupIdValues.clear();
                    groupIdValues.addAll(listIdSubjectLv3);
                }
            }
        
        if (selectedData.equals(Constants.QUALITY_REPORT_DATA_AVG_SCORE)) {
            List<ReportAVGScoreMapWeekDto> listData = new ReportLogic().findDataReportAvgScore(typePeriod, periodFrom, periodTo, selectedChanelId, selectedGroup, groupIdValues, yearFrom, yearTo, currentVenture.getIdVenture());

            buildModelReportAVGScore(listData);
            fillDataReportAvgScore(listData);
        } else if (selectedData.equals(Constants.QUALITY_REPORT_DATA_PER_CATE_PASSED_SCORE)) {
            List<ReportPerOfCateMapDto> listData = new ReportLogic().findDataReportPerOfCate(typePeriod, periodFrom, periodTo, selectedChanelId, selectedGroup, groupIdValues, yearFrom, yearTo, currentVenture.getIdVenture());
            buildModelPerOfCateReport(listData);
            fillDataReportPerOfCate(listData);
        } else if (selectedData.equals(Constants.QUALITY_REPORT_DATA_PER_ATTR_PASSED_SCORE)) {
            List<ReportPerOfAttrMapDto> listData = new ReportLogic().findDataReportPerOfAttr(typePeriod, periodFrom, periodTo, selectedChanelId, selectedGroup, groupIdValues, yearFrom, yearTo, currentVenture.getIdVenture());           
            buildModelPerOfAttrReport(listData);           
            fillDataReportPerOfAttr(listData);            
        } else {
            List<ReportTotalEvaluationMapWeekDto> listData = new ReportLogic().findDataReportTotalEvaluation(typePeriod, periodFrom, periodTo, selectedChanelId, selectedGroup, groupIdValues, yearFrom, yearTo, currentVenture.getIdVenture());
            buildModelReportTotalEvaluation(listData);
            fillDataReportTotalEvaluation(listData);
        }
        btnExportToExcel.setEnabled(true);
    }//GEN-LAST:event_btnReportActionPerformed

    private void radGroupAgentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radGroupAgentActionPerformed
        loadDataSelectGroupAgent();
    }//GEN-LAST:event_radGroupAgentActionPerformed

    private void radGroupQAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radGroupQAActionPerformed
        loadDataSelectQA();  
    }//GEN-LAST:event_radGroupQAActionPerformed

    private void radGroupTeamLeadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radGroupTeamLeadActionPerformed
        groupValuePanel.removeAll();
        groupValuePanel.setEnabled(true);
        groupValueContentPanel.setBorder(BorderFactory.createTitledBorder("Team Lead"));
        chkCheckAll.setSelected(false);
        chkCheckAll.setEnabled(true);
        selectedGroup = Constants.GROUP_TYPE_TEAM_LEAD;
        if (listAgentTeamLead == null) {
            listAgentTeamLead = new AgentLogic().findTeamLeadByVenture(currentVenture.getIdVenture());;            
        }
        for (Agent teamLead : listAgentTeamLead) {
            JCheckBox checkBox = new JCheckBox();
            checkBox.setText(teamLead.getAgentName());
            checkBox.setActionCommand(teamLead.getIdAgent() + "");

            groupValuePanel.add(checkBox);
        }
        groupValuePanel.updateUI();
    }//GEN-LAST:event_radGroupTeamLeadActionPerformed

    private void radGroupTypeOfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radGroupTypeOfActionPerformed
        
        main.addNewInternalFrame(new ChooserLevelSubject(this));
        
        if (mappSubjects == null) {
            mappSubjects = new SubjectLogic().findMapAllLevelAndChilds();
        }
        List<Subject> listSubDisplay = mappSubjects.get(selectedLevelSubject);
        
        groupValuePanel.removeAll();
        groupValuePanel.setEnabled(true);
        groupValueContentPanel.setBorder(BorderFactory.createTitledBorder("Level "+selectedLevelSubject));
        chkCheckAll.setSelected(false);
        chkCheckAll.setEnabled(true);
        selectedGroup = Constants.GROUP_TYPE_TYPE_OF_INQUIRY;
        
        for (Subject subject : listSubDisplay) {
            JCheckBox checkBox = new JCheckBox();
            checkBox.setText(subject.getSubject());
            checkBox.setActionCommand(subject.getIdSubject()+"");
            
            groupValuePanel.add(checkBox);
        }
        groupValuePanel.updateUI();
    }//GEN-LAST:event_radGroupTypeOfActionPerformed

    private void btnExportToExcelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportToExcelActionPerformed
        JFileChooser fc = new JFileChooser();
        int option = fc.showSaveDialog(this);
        
        if (option == JFileChooser.APPROVE_OPTION) {
            String filename = fc.getSelectedFile().getName();
            String path = fc.getSelectedFile().getParentFile().getPath();

            int len = filename.length();
            String ext = "";
            String file = "";

            if (len > 4) {
                ext = filename.substring(len - 4, len);
            }

            if (ext.equals(".xls")) {
                file = path + "\\" + filename;
            } else {
                file = path + "\\" + filename + ".xlsx";
            }
            String title = "Report "+selectedData;
            
            new ApplicationCommon().writeToExcell(tblResultReport, file,title,null);
        }
    }//GEN-LAST:event_btnExportToExcelActionPerformed

    private void chkCheckAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkCheckAllActionPerformed
        for (Component com : groupValuePanel.getComponents()) {
            JCheckBox checkBox = (JCheckBox) com;
            if (chkCheckAll.isSelected()) {
                checkBox.setSelected(true);
            } else {
                checkBox.setSelected(false);
            }
        }
    }//GEN-LAST:event_chkCheckAllActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bGGroup;
    private javax.swing.ButtonGroup bGPeriod;
    private javax.swing.JButton btnExportToExcel;
    private javax.swing.JButton btnReport;
    private javax.swing.JComboBox cboData;
    private javax.swing.JComboBox cboRangeFrom;
    private javax.swing.JComboBox cboRangeTo;
    private javax.swing.JCheckBox chkCheckAll;
    private javax.swing.JPanel contentLeftPanel;
    private javax.swing.JPanel groupValueContentPanel;
    private javax.swing.JPanel groupValuePanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JPanel panelChanels;
    private javax.swing.JPanel panelContentChanels;
    private javax.swing.JPanel panelGroups;
    private javax.swing.JPanel panelGroups1;
    private javax.swing.JRadioButton radGroupAgent;
    private javax.swing.JRadioButton radGroupQA;
    private javax.swing.JRadioButton radGroupTeamLead;
    private javax.swing.JRadioButton radGroupTypeOf;
    private javax.swing.JRadioButton radPeriodMonthly;
    private javax.swing.JRadioButton radPeriodQuarterly;
    private javax.swing.JRadioButton radPeriodWeekly;
    private javax.swing.JPanel rightPanel;
    private com.toedter.calendar.JYearChooser spinnerYearFrom;
    private com.toedter.calendar.JYearChooser spinnerYearTo;
    private javax.swing.JTable tblResultReport;
    // End of variables declaration//GEN-END:variables
    private void loadDataChanels() {
        List<ScorecardType> types = new ScorecardTypeLogic().findAllScrorecardType();
        ButtonGroup bGType = new ButtonGroup();
        // add radio All
        JRadioButton radAll = new JRadioButton();
        radAll.setText("All");
        radAll.setActionCommand(-1+"");
        radAll.setSelected(true);
        bGType.add(radAll);
        panelChanels.add(radAll);
        
        for (ScorecardType type : types) {
            JRadioButton rad = new JRadioButton();
            rad.setText(type.getType());
            rad.setActionCommand(type.getIdScorecardType()+"");
            rad.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    selectedChanelId = Integer.parseInt(e.getActionCommand());
                }
            });
            bGType.add(rad);
            panelChanels.add(rad);
        }
    }
    
    private void loadDataDatas() {
        cboData.removeAllItems();
        cboData.addItem(Constants.QUALITY_REPORT_DATA_AVG_SCORE);
        cboData.addItem(Constants.QUALITY_REPORT_DATA_PER_CATE_PASSED_SCORE);
        cboData.addItem(Constants.QUALITY_REPORT_DATA_PER_ATTR_PASSED_SCORE);
        cboData.addItem(Constants.QUALITY_REPORT_DATA_TOTAL_NUMBER_OF_EVALUATION);
    }
    
    private void loadWeeklyFromTo() {
        cboRangeFrom.removeAllItems();
        cboRangeTo.removeAllItems();
//        Map<Integer, Date[]> mapWeekDate = DateUtils.getMapWeekInCurrentYear();
//        for (Integer week : mapWeekDate.keySet()) {
//            cboRangeFrom.addItem(week);
//            cboRangeTo.addItem(week);
//        }
        int year = spinnerYearFrom.getYear();
        Map<Integer, Date[]> mapWeekInYear = DateUtils.getMapWeekInYear(year);
        SimpleDateFormat formatOnlyYear = new SimpleDateFormat("yyyy");
        for (int week : mapWeekInYear.keySet()) {
            
            Date dateFrom = mapWeekInYear.get(week)[0];                      
            Date dateTo = mapWeekInYear.get(week)[1];
            SimpleDateFormat formatter = new SimpleDateFormat("MMM-dd");
            String labelTime = "";
            if (Integer.parseInt(formatOnlyYear.format(dateTo)) <= year) {
                labelTime = "Week "+week+" "+formatter.format(dateFrom)+" to "+formatter.format(dateTo);
            } else {
                labelTime = "Week "+week+" ("+(year+1)+") "+formatter.format(dateFrom)+" to "+formatter.format(dateTo);
            }
            cboRangeFrom.addItem(new WeekComboItem(week, labelTime));
            cboRangeTo.addItem(new WeekComboItem(week, labelTime));
        }
    }
    
    private void loadMonthFromTo() {
        cboRangeFrom.removeAllItems();
        cboRangeTo.removeAllItems();
        cboRangeFrom.addItem(new MonthComboItem(1, Constants.MONTH_NAME_1));
        cboRangeTo.addItem(new MonthComboItem(1, Constants.MONTH_NAME_1));
        mapMonth.put(1, Constants.MONTH_NAME_1);
        cboRangeFrom.addItem(new MonthComboItem(2, Constants.MONTH_NAME_2));
        cboRangeTo.addItem(new MonthComboItem(2, Constants.MONTH_NAME_2));
        mapMonth.put(2, Constants.MONTH_NAME_2);
        cboRangeFrom.addItem(new MonthComboItem(3, Constants.MONTH_NAME_3));
        cboRangeTo.addItem(new MonthComboItem(3, Constants.MONTH_NAME_3));
        mapMonth.put(3, Constants.MONTH_NAME_3);
        cboRangeFrom.addItem(new MonthComboItem(4, Constants.MONTH_NAME_4));
        cboRangeTo.addItem(new MonthComboItem(4, Constants.MONTH_NAME_4));
        mapMonth.put(4, Constants.MONTH_NAME_4);
        cboRangeFrom.addItem(new MonthComboItem(5, Constants.MONTH_NAME_5));
        cboRangeTo.addItem(new MonthComboItem(5, Constants.MONTH_NAME_5));
        mapMonth.put(5, Constants.MONTH_NAME_5);
        cboRangeFrom.addItem(new MonthComboItem(6, Constants.MONTH_NAME_6));
        cboRangeTo.addItem(new MonthComboItem(6, Constants.MONTH_NAME_6));
        mapMonth.put(6, Constants.MONTH_NAME_6);
        cboRangeFrom.addItem(new MonthComboItem(7, Constants.MONTH_NAME_7));
        cboRangeTo.addItem(new MonthComboItem(7, Constants.MONTH_NAME_7));
        mapMonth.put(7, Constants.MONTH_NAME_7);
        cboRangeFrom.addItem(new MonthComboItem(8, Constants.MONTH_NAME_8));
        cboRangeTo.addItem(new MonthComboItem(8, Constants.MONTH_NAME_8));
        mapMonth.put(8, Constants.MONTH_NAME_8);
        cboRangeFrom.addItem(new MonthComboItem(9, Constants.MONTH_NAME_9));
        cboRangeTo.addItem(new MonthComboItem(9, Constants.MONTH_NAME_9));
        mapMonth.put(9, Constants.MONTH_NAME_9);
        cboRangeFrom.addItem(new MonthComboItem(10, Constants.MONTH_NAME_10));
        cboRangeTo.addItem(new MonthComboItem(10, Constants.MONTH_NAME_10));
        mapMonth.put(10, Constants.MONTH_NAME_10);
        cboRangeFrom.addItem(new MonthComboItem(11, Constants.MONTH_NAME_11));
        cboRangeTo.addItem(new MonthComboItem(11, Constants.MONTH_NAME_11));
        mapMonth.put(11, Constants.MONTH_NAME_11);
        cboRangeFrom.addItem(new MonthComboItem(12, Constants.MONTH_NAME_12));
        cboRangeTo.addItem(new MonthComboItem(12, Constants.MONTH_NAME_12));
        mapMonth.put(12, Constants.MONTH_NAME_12);
    }
    
    private void loadQuarterFromTo() {
        cboRangeFrom.removeAllItems();
        cboRangeTo.removeAllItems();
        cboRangeFrom.addItem(new QuarterComboItem(1, Constants.QUARTER_1));
        cboRangeTo.addItem(new QuarterComboItem(1, Constants.QUARTER_1));
        cboRangeFrom.addItem(new QuarterComboItem(2, Constants.QUARTER_2));
        cboRangeTo.addItem(new QuarterComboItem(2, Constants.QUARTER_2));
        cboRangeFrom.addItem(new QuarterComboItem(3, Constants.QUARTER_3));
        cboRangeTo.addItem(new QuarterComboItem(3, Constants.QUARTER_3));
        cboRangeFrom.addItem(new QuarterComboItem(4, Constants.QUARTER_4));
        cboRangeTo.addItem(new QuarterComboItem(4, Constants.QUARTER_4));
    }
    
    private void buildModelReportAVGScore(List<ReportAVGScoreMapWeekDto> listData) {
        DefaultTableModel model = new DefaultTableModel();
        tblResultReport.setModel(model);
        
        if (selectedGroup != Constants.GROUP_TYPE_TYPE_OF_INQUIRY) {
            model.addColumn("Rank");
        }
        if (selectedGroup == Constants.GROUP_TYPE_AGENT) {
            model.addColumn("Agent Name");
        } else if (selectedGroup == Constants.GROUP_TYPE_QA) {
            model.addColumn("QA Name");
        } else if (selectedGroup == Constants.GROUP_TYPE_TEAM_LEAD) {
            model.addColumn("Team Leader Name");
        } else {
            model.addColumn("Subject");
        }
        
        if (listData.size() > 0) {
            for (String key : listData.get(0).getMapWeekAndScore().keySet()) {
                if (typePeriod == Constants.PERIOD_TYPE_WEEKLY) {
                    model.addColumn("Week " + key.split("-")[0]);
                } else if (typePeriod == Constants.PERIOD_TYPE_MONTHLY){
                    model.addColumn(mapMonth.get(Integer.parseInt(key.split("-")[0]))+" ("+key.split("-")[1]+")");
                } else {
                    model.addColumn("Quarter " + key.split("-")[0] +" ("+key.split("-")[1]+")");
                }
            }
        }
        model.addColumn("Average");

    }
    
    private void buildModelPerOfCateReport(List<ReportPerOfCateMapDto> listData) {
        DefaultTableModel model = new DefaultTableModel();
        tblResultReport.setModel(model);
        
        if (selectedGroup == Constants.GROUP_TYPE_AGENT) {
            model.addColumn("Agent Name");
        } else if (selectedGroup == Constants.GROUP_TYPE_QA) {
            model.addColumn("QA Name");
        } else if (selectedGroup == Constants.GROUP_TYPE_TEAM_LEAD) {
            model.addColumn("Team Leader Name");
        } else {
            model.addColumn("Subject");
        }

        if (listData.size() > 0) {
            for (String key : listData.get(0).getMapGaroupAttrValue().keySet()) {
                if (!key.equals("")) {
                    model.addColumn(key);
                }
            }
        }        
    }
    
    private void buildModelPerOfAttrReport(List<ReportPerOfAttrMapDto> listData) {
        DefaultTableModel model = new DefaultTableModel();
        tblResultReport.setModel(model);
        tblResultReport.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        
        if (selectedGroup == Constants.GROUP_TYPE_AGENT) {
            model.addColumn("Agent Name");
        } else if (selectedGroup == Constants.GROUP_TYPE_QA) {
            model.addColumn("QA Name");
        } else if (selectedGroup == Constants.GROUP_TYPE_TEAM_LEAD) {
            model.addColumn("Team Leader Name");
        } else {
            model.addColumn("Subject");
        }
//        int columnIndex = 1;
        if (listData.size() > 0) {
            for (String key : listData.get(0).getMapAttrValue().keySet()) {
                if (!key.equals("")) {                   
                    model.addColumn(key);
//                    tblResultReport.getColumnModel().getColumn(columnIndex).setPreferredWidth(400);
//                    tblResultReport.getColumnModel().getColumn(columnIndex).setMinWidth(400);
//                    tblResultReport.getColumnModel().getColumn(columnIndex).setMaxWidth(400);
//                    columnIndex++;
                }
            }
        }        
    }
    
    private void buildModelReportTotalEvaluation(List<ReportTotalEvaluationMapWeekDto> listData) {
        DefaultTableModel model = new DefaultTableModel();
        tblResultReport.setModel(model);
        model.addColumn("Rank");
        if (selectedGroup == Constants.GROUP_TYPE_QA) {
            model.addColumn("QA Name");
        } else {
            model.addColumn("Agent Name");
        }

        if (listData.size() > 0) {
            for (String key : listData.get(0).getMapWeekAndScore().keySet()) {
                if (typePeriod == Constants.PERIOD_TYPE_WEEKLY) {
                    model.addColumn("Week " + key.split("-")[0]);
                } else if (typePeriod == Constants.PERIOD_TYPE_MONTHLY){
                    model.addColumn(mapMonth.get(Integer.parseInt(key.split("-")[0]))+" ("+key.split("-")[1]+")");
                } else {
                    model.addColumn("Quarter " + key.split("-")[0] +" ("+key.split("-")[1]+")");
                }
            }
        }
        model.addColumn("Average");

    }
    
    private void fillDataReportAvgScore(List<ReportAVGScoreMapWeekDto> listData) {
        if (listData.size() > 0) {
            int row = 0;
            DefaultTableModel model = (DefaultTableModel) tblResultReport.getModel();

            Object[][] data = new Object[listData.size()][model.getColumnCount()];
            String[] columnName = new String[model.getColumnCount()];
            for (int i = 0; i < model.getColumnCount(); i++) {
                columnName[i] = model.getColumnName(i);
            }
            DecimalFormat df = new DecimalFormat("##.#");
            
            for (ReportAVGScoreMapWeekDto result : listData) {
                int col = 0;
                if (selectedGroup != Constants.GROUP_TYPE_TYPE_OF_INQUIRY) {
                    data[row][col] = "";
                    col++;
                }
                data[row][col] = result.getAgentName();
                col++;
                
                double avgScore = 0;
                int numberColumnDynamic = 0;
                for (String week : result.getMapWeekAndScore().keySet()) {
                    data[row][col] = df.format(result.getMapWeekAndScore().get(week));
                    avgScore += result.getMapWeekAndScore().get(week);
                    col++;
                    numberColumnDynamic++;
                }
                data[row][col] = df.format(avgScore / numberColumnDynamic);

                row++;
            }
            final int numberCol = model.getColumnCount();
            Arrays.sort(data, new Comparator<Object[]>() {
                @Override
                public int compare(final Object[] entry1, final Object[] entry2) {
                    final Double value1 = Double.valueOf(entry1[numberCol - 1].toString());
                    final Double value2 = Double.valueOf(entry2[numberCol - 1].toString());;
                    return value2.compareTo(value1);
                }
            });
            model.setDataVector(data, columnName);
            // set rank
            if (selectedGroup != Constants.GROUP_TYPE_TYPE_OF_INQUIRY) {
                for (int i = 0; i < data.length; i++) {
                    model.setValueAt(i + 1, i, 0);
                }
            }
        }
    }
    
    private void fillDataReportPerOfCate(List<ReportPerOfCateMapDto> listData) {
        if (listData.size() > 0) {
            int row = 0;
            DefaultTableModel model = (DefaultTableModel) tblResultReport.getModel();
            
            Object[][] data = new Object[listData.size()][model.getColumnCount()];
            String[] columnName = new String[model.getColumnCount()];
            for (int i = 0; i < model.getColumnCount(); i++) {
                columnName[i] = model.getColumnName(i);
            }
            DecimalFormat df = new DecimalFormat("##.#");
            for (ReportPerOfCateMapDto result : listData) {
                data[row][0] = result.getAgentName();
                int columnDynamic = 1;                                
                for (String groupAttr : result.getMapGaroupAttrValue().keySet()) {
                    if (!groupAttr.equals("")) {
                        
                        data[row][columnDynamic] = df.format(result.getMapGaroupAttrValue().get(groupAttr) * 100) + "%";                  
                        columnDynamic++;            
                    }
                }               
                row++;
            }
            model.setDataVector(data, columnName);
        }
    }
    
    private void fillDataReportPerOfAttr(List<ReportPerOfAttrMapDto> listData) {
        if (listData.size() > 0) {
            int row = 0;
            DefaultTableModel model = (DefaultTableModel) tblResultReport.getModel();
            
            Object[][] data = new Object[listData.size()][model.getColumnCount()];
            String[] columnName = new String[model.getColumnCount()];
            for (int i = 0; i < model.getColumnCount(); i++) {
                columnName[i] = model.getColumnName(i);
            }
            DecimalFormat df = new DecimalFormat("##.#");
            for (ReportPerOfAttrMapDto result : listData) {
                data[row][0] = result.getAgentName();
                int columnDynamic = 1;                                
                for (String groupAttr : result.getMapAttrValue().keySet()) {
                    if (!groupAttr.equals("")) {
                        
                        data[row][columnDynamic] = df.format(result.getMapAttrValue().get(groupAttr)) + "%";                  
                        columnDynamic++;            
                    }
                }               
                row++;
            }
            model.setDataVector(data, columnName);
        }
    }
    
    private void fillDataReportTotalEvaluation(List<ReportTotalEvaluationMapWeekDto> listData) {
        if (listData.size() > 0) {
            int row = 0;
            DefaultTableModel model = (DefaultTableModel) tblResultReport.getModel();

            Object[][] data = new Object[listData.size()][model.getColumnCount()];
            String[] columnName = new String[model.getColumnCount()];
            for (int i = 0; i < model.getColumnCount(); i++) {
                columnName[i] = model.getColumnName(i);
            }
            DecimalFormat df = new DecimalFormat("##.#");
            for (ReportTotalEvaluationMapWeekDto result : listData) {
                data[row][0] = "";
                data[row][1] = result.getAgentName();

                int columnDynamic = 2;
                double avgScore = 0;
                int numberColumnDynamic = 0;
                for (String week : result.getMapWeekAndScore().keySet()) {
                    data[row][columnDynamic] = df.format(result.getMapWeekAndScore().get(week));
                    avgScore += result.getMapWeekAndScore().get(week);
                    columnDynamic++;
                    numberColumnDynamic++;
                }
                data[row][columnDynamic] = df.format(avgScore / numberColumnDynamic);

                row++;
            }
            final int numberCol = model.getColumnCount();
            Arrays.sort(data, new Comparator<Object[]>() {
                @Override
                public int compare(final Object[] entry1, final Object[] entry2) {
                    final Double value1 = Double.valueOf(entry1[numberCol - 1].toString());
                    final Double value2 = Double.valueOf(entry2[numberCol - 1].toString());;
                    return value2.compareTo(value1);
                }
            });
            model.setDataVector(data, columnName);

            for (int i = 0; i < data.length; i++) {
                model.setValueAt(i + 1, i, 0);
            }
        }
    }
    
    private void loadDataSelectQA() {
        groupValuePanel.removeAll();
        groupValuePanel.setEnabled(true);
        groupValueContentPanel.setBorder(BorderFactory.createTitledBorder("QA"));
        chkCheckAll.setSelected(false);
        chkCheckAll.setEnabled(true);
        selectedGroup = Constants.GROUP_TYPE_QA;
        if (listAgentQA == null) {
            listAgentQA = new AgentLogic().findQAByVenture(currentVenture.getIdVenture());
            
//            Collections.sort(listAgentQA, new Comparator<Agent>() {
//                @Override
//                public int compare(Agent agent1, Agent agent2) {
//                    return agent1.getAgentName().compareTo(agent2.getAgentName());
//                }
//            });
        }
        
        for (Agent qa : listAgentQA) {
            JCheckBox checkBox = new JCheckBox();
            checkBox.setText(qa.getAgentName());
            checkBox.setActionCommand(qa.getIdAgent()+"");
            
            groupValuePanel.add(checkBox);
        }
        groupValuePanel.updateUI();
    }
    
    private void loadDataSelectGroupAgent() {
        groupValuePanel.removeAll();
        groupValuePanel.setEnabled(true);
        groupValueContentPanel.setBorder(BorderFactory.createTitledBorder("Agent"));
        chkCheckAll.setSelected(false);
        chkCheckAll.setEnabled(true);
        selectedGroup = Constants.GROUP_TYPE_AGENT;
        
        if (listAgent == null) {
            listAgent = new AgentLogic().findAgentsByVenture(currentVenture.getIdVenture());
        }
        
        for (Agent agent : listAgent) {
            JCheckBox checkBox = new JCheckBox();
            checkBox.setText(agent.getAgentName());
            checkBox.setActionCommand(agent.getIdAgent()+"");
            
            groupValuePanel.add(checkBox);
        }
        groupValuePanel.updateUI();
    }      
    
    private List<Subject> filterListChild(List<Subject> list, Integer idParent) {
        List<Subject> listFiltered = new ArrayList<>();
        for (Subject subject : list) {
            if (subject.getIdParent().compareTo(idParent) == 0) {
                listFiltered.add(subject);
            }            
        }       
        return listFiltered;
    }
    
    public int getSelectedLevelSubject() {
        return selectedLevelSubject;
    }

    public void setSelectedLevelSubject(int selectedLevelSubject) {
        this.selectedLevelSubject = selectedLevelSubject;
    }
    
    
}
